import QtQuick 2.0
import QtQuick.Controls 2.5

import "../basic"

ComboBox {
    id: fontSelectorCbx
    property real indicatorRotation: 90
    property int aveHeight: count < 8 ? count * 40 : 280
    property string text: displayText
    property bool isOpen: false
    model: [ "默认","仿宋","宋体","楷体","黑体","华文中宋","华文仿宋","华文宋体","华文新魏","华文楷体"]
    background: Rectangle {
        anchors.fill: parent
        radius: parent.height / 2
        border.width: 1
        border.color: "#28282e"
        color: "#1a1a20"
        MouseArea {
            anchors.fill: parent
            hoverEnabled: true
            onEntered: {
                cursorShape = Qt.PointingHandCursor
            }
            onExited: {
                cursorShape = Qt.ArrowCursor
            }
            onClicked: {
                if (fontSelectorCbx.isOpen) {
                    fontSelectorCbx.popup.close()
                } else {
                    fontSelectorCbx.popup.open()    // 打开下拉框
                }
            }
        }
    }
    // 右侧图标设置
    indicator: Label {
        text: ">"
        font.pixelSize: 22
        font.bold: true
        font.family: BasicConfig.heiTiFont
        color: "white"
        anchors.right: parent.right
        anchors.rightMargin: 10
        anchors.verticalCenter: parent.verticalCenter
        rotation: fontSelectorCbx.indicatorRotation
    }
    contentItem: Text {
        text: fontSelectorCbx.text
        font.pixelSize: 16
        color: "white"
        font.family: BasicConfig.commFont
        font.bold: true
        verticalAlignment: Text.AlignVCenter
        anchors.left: parent.left
        anchors.leftMargin: 10
    }
    popup: Popup {
        y: parent.height + 5
        width: parent.width
        height: fontSelectorCbx.aveHeight
        onOpened: {
            fontSelectorCbx.indicatorRotation = -90
            fontSelectorCbx.isOpen = true
        }
        onClosed: {
            fontSelectorCbx.indicatorRotation = 90
            fontSelectorCbx.isOpen = false
        }
        background: Rectangle {
            anchors.fill: parent
            radius: 10
            color: "#2d2d37"
            clip: true
            ListView {
                anchors.fill: parent
                model: fontSelectorCbx.model
                ScrollBar.vertical: ScrollBar {
                    id: cusScrollBar
                    width: 10
                    policy: ScrollBar.AsNeeded
                    contentItem: Rectangle {
                        visible: parent.active
                        color: "#42424b"
                    }
                }
                delegate: Rectangle {
                    width: fontSelectorCbx.width - 10
                    height: 40
                    color: "transparent"
                    Text {
                        text: modelData
                        font.pixelSize: 16
                        anchors.left: parent.left
                        anchors.leftMargin: 10
                        anchors.verticalCenter: parent.verticalCenter
                        font.family: BasicConfig.commFont
                        color: "#C0C0C4"
                    }
                    MouseArea {
                        anchors.fill: parent
                        hoverEnabled: true
                        onEntered: {
                            parent.color = "#393943"
                            cursorShape = Qt.PointingHandCursor
                        }
                        onExited: {
                            parent.color = "transparent"
                            cursorShape = Qt.ArrowCursor
                        }
                        onClicked: {
                            fontSelectorCbx.text = modelData
                            fontSelectorCbx.popup.close()
                        }
                    }
                }
            }
        }
    }
}
